<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"/>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $configs['title']; ?></title>
</head>
<link href="css/tool.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src='js/jquery-1.2.6.min.js'></script>
<body>
<br />
<div>
	<div>
		<form id="urlform" name="urlform" action="" method="POST">	
		<table width="400" align="left" cellpadding="0" cellspacing="0" id="mytable">
		<?php 
			if ( $_COOKIE['username'] != 'cardadmin'){
		?>
			<tr>
				<th colspan="3">主数据库选择 [<a href="index.php?act=logout">退出</a>] [<a href="#" onclick="editConfig();">修改配置</a>] [<a href="#" onclick="backupConfig();">备份配置</a>] [<a href="dctools/dctools.php">DC专用</a>]</th>
			</tr>
			<tr>
				<td colspan="3">			
				&nbsp;&nbsp;<select id='maindb'>
				<option value="0">请选择主数据库</option>
				<?php
				$dbns = msort($g_db_conf[$version]);
				foreach ($dbns as $key => $value){
					echo "<option value={$key}>",$key,'服务器数据库</option>';
				}
				?>			
				</select>&nbsp;<b>	<?=$_SERVER['SERVER_NAME'];	?>版本</b></td>
			</tr>
			<tr>
				<th colspan="4">对比数据库选择</th>
			</tr>
			<tr>
				<td><input name='chkAll' type='checkbox' id='chkAll' onclick='CheckAll(this.form)' value='checkbox'/> 全选</td>
				<td>数据库名称</td>
			</tr>
			<?php
			$dbns = msort($g_db_conf[$version]);
			foreach ($dbns as $key => $value){
			?>
				<tr><td><input type='checkbox' name='dbids' value=<?php echo $key;?> /></td><td><a href="comparetable.php?dbId=<?php echo $key;?>"><?php echo $key,'服务器数据库';?></a>&nbsp;&nbsp;[<a href="#" onclick="showExport('<?php echo $key;?>')">导出</a>]</td></tr>
			<?php
			}
			?>
			<tr>
				<th colspan="3">&nbsp;&nbsp;<input name="compare_db" type="button" id="compare_db" value="对比选中项" onclick="compareDbs('comparedb');"/>&nbsp;&nbsp;<input name="compare_db" type="button" id="compare_db" value="同步数据" onclick="compareDbs('tables');"/>
				&nbsp;&nbsp;<input name="compare_db" type="button" id="compare_db" value="逐表对比" onclick="compareDbsNew('comparedb', 1);"/>
				&nbsp;&nbsp;<input type="button" value="远程对比" onclick="window.location.href='comparenet.php'"/>
				<br>&nbsp;<br>&nbsp;
				<input name="compare_db" type="button" id="compare_db" value="固定表数据同步" onclick="if(confirm('确定同步固定表的数据吗?')) javascript:allAsynDb();"/>
				&nbsp;&nbsp;<input name="compare_db" type="button" id="compare_db" value="权限表数据同步" onclick="if(confirm('确定同步权限表的数据吗?')) javascript:allAsynDb1();">
				&nbsp;&nbsp;<input name="compare_db" type="button" id="compare_db" value="一键同步" onclick="if(confirm('确定使用一键同步吗?')) javascript:allAotuAsynDb();"/>
				<br>&nbsp;<br>&nbsp;
				<input name="compare_db" type="button" id="compare_db" value="SQL语句执行同步" onclick="if(confirm('确定SQL语句执行同步吗?')) javascript:allSqlDb();"/>
				</th>
			</tr>
			<?php
			}else{
			?>
			<tr>
				<th colspan="3">主数据库选择 [<a href="index.php?act=logout">退出</a>][<a href="dctools/dctools.php">DC专用</a>]</td>
			</tr>
			<tr>
				<td colspan="3">			
				&nbsp;&nbsp;<select id='maindb'>
				<option value="0">请选择主数据库</option>
				<?php
				foreach ($g_db_conf[$version] as $key => $value){
					echo "<option value={$key}>",$key,'服务器数据库</option>';
				}
				?>			
				</select>&nbsp;<b>	<?=$_SERVER['SERVER_NAME'];	?>版本</b></td>
			</tr>
			<tr>
				<th colspan="4">对比数据库选择</th>
			</tr>
			<tr>
				<td><input name='chkAll' type='checkbox' id='chkAll' onclick='CheckAll(this.form)' value='checkbox'> 全选</td>
				<td>数据库名称</td>
			</tr>
			<?php
			foreach ($g_db_conf[$version] as $key => $value){
			?>
				<tr><td><input type='checkbox' name='dbids' value=<?php echo $key;?> /></td><td><?php echo $key,'服务器数据库';?></td></tr>
			<?php
			}
			?>
			<tr>
				<th colspan="3">
				&nbsp;&nbsp;<input name="compare_db" type="button" id="compare_db" value="权限表数据同步" onclick="if(confirm('确定同步权限表的数据吗?')) javascript:allAsynDb1();">
				</th>
			</tr>
			<?php
			}
			?>
		</table>
		</form>
	</div>
	<div id="viewdb" align="center"></div>
	<div id="loaddiv" align="center" style="display:none;"><img src="images/loading.gif"/>正在操作数据库,请稍候...</div>
	<div id="mdiv"></div>
</div>

	
<script type="text/javascript">
//比较数据库
function compareDbs(type){
	if ($('#maindb').val() == 0){
		alert('请选择主数据库!');
		return;
	}

	var str="";
	$("[name='dbids'][checked]").each(function(){
		str+=$(this).val()+"|";
	})

	$.ajax({
		type:'POST',
		timeout:180000,	
		url:'ajax/comparedb.ajax.php',			
		data:"act="+type+"&dbId="+str+"&dbmainId="+$('#maindb').val(),
		beforeSend:function () {
			$('#loaddiv').show();			
		},		
		success:function(msg){
			if (msg == '请选择需要对比的数据库'){
				alert(msg);
			}else{				
				$('#viewdb').html(msg);
			}
			$('#loaddiv').hide();
		},
		error:function(){
			alert('超时了，请检查服务器设置是否正确！');
			$('#loaddiv').hide();
			return;
		}
	})
}

//一键同步
function allAotuAsynDb(){
	if ($('#maindb').val() == 0){
		alert('请选择主数据库!');
		return;
	}

	var str="";
	$("[name='dbids'][checked]").each(function(){
		str+=$(this).val()+"|";
	})

	$.ajax({
		type:'POST',
		timeout:180000,	
		url:'ajax/comparedb.ajax.php',			
		data:"act=autoasyn&dbId="+str+"&dbmainId="+$('#maindb').val(),
		beforeSend:function () {
			$('#loaddiv').show();			
		},		
		success:function(msg){
			if (msg == '请选择需要对比的数据库') {
				alert(msg);
			} else {			
				$('#viewdb').html(msg);
			}
			$('#loaddiv').hide();
		},
		error:function(){
			alert('超时了，请检查服务器设置是否正确！');
			$('#loaddiv').hide();
			return;
		}
	})
}

//比较数据库 逐个表对比
function compareDbsNew(type, first){
	if ($('#maindb').val() == 0){
		alert('请选择主数据库!');
		return;
	}
	var maindb = $('#maindb').val();
	var str="";
	$("[name='dbids'][checked]").each(function(){
		str+=$(this).val();
	})

	$.ajax({
		type:'POST',
		timeout:30000,	
		url:'ajax/comparedbnew.ajax.php',			
		data:"act="+type+"&dbId="+str+"&dbmainId="+$('#maindb').val()+"&first="+first,
		dataType:'json',
		beforeSend:function () {
			$('#loaddiv').show();			
		},		
		success:function(msg){
			if (msg == '请选择需要对比的数据库'){
				alert(msg);
			}else{
				//$('#viewdb').html(msg);
				//				for ( x in msg ){
				//					callbackcompareDbsNew(msg[x], str, $('#maindb').val());
				//					//$("#mdiv").append('<div style="width:0px;margin-top:0px;margin-right:0px;float:left"><iframe src="ajax/comparedbnew2.ajax.php?act=comparetable&tablename='+msg[x]+'" frameborder="0" height="0" width="0"></iframe></div>');
				//				}
				//if( msg.num != 'end' || msg.num == 'all'){
				if( msg.num==0 || msg.num == 'all'){
					var headercontent = "<div><input type='checkbox' id='is_back' name='is_back' value='1' ><font color=red >是否备份</font>&nbsp;&nbsp;<input type='checkbox' id='is_comment' name='is_comment' value='1' ><font color=red >是否导入注释</font><b>主数据库为:"+$('#maindb').val()+",以下为选中数据库的比较结果</b></div>";
					headercontent += "<div id=\"con_one_"+msg.num+"\" ><form><table width=\"800\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" id=\"myTable\"><tr><th><input name='chkAll' type='checkbox' id='chkAll' onclick='CheckAll(this.form)' value='checkbox'> 全选</th><th>表名</th><th>数据库<font color=red>"+str+"</font></th><th>字段缺少</th><th>字段多余</th></tr></table></form></div>";
					$('#viewdb').html(headercontent);
				}
				if (msg.state==1 && msg.content != ''){
					$('#myTable').append(msg.content);
				}
				if ( msg.num != 'end' && msg.num != 'all' ){
					compareDbsNew('comparedb', 0);
				}else{
					var html = "<tr><td colspan='6'>[数据库"+str+"]&nbsp;&nbsp;";;
					html += "<a href='#' onclick=\"if(confirm('确定添加缺少的表吗?')) asynDb('add', '"+str+"', '"+maindb+"');\">添加缺少的表</a> &nbsp;&nbsp;";
					html += "<a href='#' onclick=\"if(confirm('确定删除多余的表吗?')) asynDb('drop', '"+str+"', '"+maindb+"');\">删除多余的表</a> &nbsp;&nbsp;";
					html += "<a href='#' onclick=\"if(confirm('确定同步字段吗?')) asynDb('field', '"+str+"', '"+maindb+"');\">同步字段</a> &nbsp;&nbsp;";
					html += "<a href='#' onclick=\"if(confirm('确定删除多余字段吗?')) asynDb('del_field', '"+str+"', '"+maindb+"');\">删除字段</a> &nbsp;&nbsp;";
					$('#myTable').append(html);
					alert('扫描对比完成');
					$('#loaddiv').hide();
				}

			}
			
		},
		error:function(){
//			alert('超时了，请检查服务器设置是否正确！');
			compareDbsNew('comparedb', 0);
			$('#loaddiv').hide();
			return;
		}
	})
}

//比较数据库 对比单张表数据并返回
function callbackcompareDbsNew(table, str , main){
alert('a');
}

//配置界面
function editConfig(){
	$.ajax({
		type:'POST',
		async:false,
		url:'editconfig.php?act=ajax',
		beforeSend:function () {
			$('#loaddiv').show();
		},
		success:function(html){
			$('#viewdb').html(html);
			$('#loaddiv').hide();
		}
	})
}

//备份配置
function backupConfig(){
	$.ajax({
		type:'POST',
		async:false,
		url:'ajax/comparedb.ajax.php',
		data:"act=backup",
		success:function(msg){
			alert('备份成功');
		}
	})
}

//导出界面
function showExport(dbId){
	$.ajax({
		type:'POST',
		async:false,
		url:'dataexport.php?act=ajax&dbId='+dbId,
		beforeSend:function () {
			$('#loaddiv').show();
		},
		success:function(html){
			$('#viewdb').html(html);
			$('#loaddiv').hide();
		}
	})
}
//同步
function asynDb(type, db, main, ck){
    ck = ck || 0;
	var str="";
	$("[name='dbids_"+db+"'][checked]").each(function(){
		str+=$(this).val()+"|";
	})

	$.ajax({
		type:'POST',
		timeout:180000,	
		async:false,
		url:'ajax/comparedb.ajax.php',
		data:"act="+type+"&tabId="+str+"&dbId="+db+"&dbmainId="+main+"&is_back="+$('#is_back[checked]').val() + "&is_comment="+$('#is_comment[checked]').val()+"&ck="+ck,
		beforeSend:function () {
			$('#loaddiv').show();
		},
		success:function(msg){
		    cfv = msg.substr(0, 2);
		    if (cfv == 'cf' && ck == 0) {
		        if(confirm(msg.substr(2))){
		            asynDb(type, db, main, 1);
		        }
		        $('#loaddiv').hide();    
		        return false;
		    }
			$('#loaddiv').hide();
			alert(msg);
		},
		error:function(){
			alert('ajax请求错误');
			$('#loaddiv').hide();
			return;
		}
	})
}
//首页增加直接选择数据库 同步固定表
function allAsynDb(ck){
    ck = ck || 0;
	if ($('#maindb').val() == 0){
		alert('请选择主数据库!');
		return;
	}
	
	var str="";
	$("[name='dbids'][checked]").each(function(){
		str+=$(this).val()+"|";
	})
	if ( str == "" ){
		alert('请选择要同步的数据库!');
		return;
	}
	
	$.ajax({
		type:'POST',
		timeout:180000,	
		async:false,
		url:'ajax/comparedb.ajax.php',
		data:"act=newspecialdata&dbId="+str + '&dbmainId=' + $('#maindb').val()+"&ck="+ck,
		beforeSend:function () {
			$('#loaddiv').show();
		},
		success:function(msg){
		    cfv = msg.substr(0, 2);
		    if (cfv == 'cf' && ck == 0) {
		        if(confirm(msg.substr(2))){
		            allAsynDb(1);
		        }
		        $('#loaddiv').hide();    
		        return false;
		    }
			$('#loaddiv').hide();
			$('#viewdb').html(msg);
		},
		error:function(){
			alert('ajax请求错误');
			$('#loaddiv').hide();
			return;
		}
	})
}

//SQL直接执行同步
function allSqlDb(){
	if ($('#maindb').val() == 0){
		alert('请选择主数据库!');
		return;
	}
	
	var str="";
	$("[name='dbids'][checked]").each(function(){
		str+=$(this).val()+"|";
	})
	if ( str == "" ){
		alert('请选择要同步的数据库!');
		return;
	}
	
	$.ajax({
		type:'POST',
		timeout:180000,	
		async:false,
		url:'ajax/comparedb.ajax.php',
		data:"act=sqldb&dbId="+str + '&dbmainId=' + $('#maindb').val(),
		beforeSend:function () {
			$('#loaddiv').show();
		},
		success:function(msg){
			$('#loaddiv').hide();
			$('#viewdb').html(msg);
		},
		error:function(){
			alert('ajax请求错误');
			$('#loaddiv').hide();
			return;
		}
	})
}

//首页增加直接同步权限表
function allAsynDb1(){
	if ($('#maindb').val() == 0){
		alert('请选择主数据库!');
		return;
	}
	
	var str="";
	$("[name='dbids'][checked]").each(function(){
		str+=$(this).val()+"|";
	})
	if ( str == "" ){
		alert('请选择要同步的数据库!');
		return;
	}
	
	$.ajax({
		type:'POST',
		timeout:180000,	
		async:false,
		url:'ajax/comparedb.ajax.php',
		data:"act=newspecialdata1&dbId="+str + '&dbmainId=' + $('#maindb').val(),
		beforeSend:function () {
			$('#loaddiv').show();
		},
		success:function(msg){
			msg = msg+'<br><a href ="http://stest.long.593wan.com/tools/mt/mc.php?k=dragoncall&t=tools_cate,tools_group,tools_url,tools_user" target="_blank">中文刷新缓存</a>';
			msg = msg+'<br><a href ="http://stest.dc.gamedp.com/tools/mt/mc.php?k=dragoncall&t=tools_cate,tools_group,tools_url,tools_user" target="_blank">英文刷新缓存</a>';
			$('#loaddiv').hide();
			$('#viewdb').html(msg);
		},
		error:function(){
			alert('ajax请求错误');
			$('#loaddiv').hide();
			return;
		}
	})
}
//全选
function unselectall(){
	if(document.myform.chkAll.checked){
		document.myform.chkAll.checked = document.myform.chkAll.checked&0;
	}
}
function CheckAll(form){
	for (var i=0;i<form.elements.length;i++){
		var e = form.elements[i];
		if (e.Name != 'chkAll'&&e.disabled==false)
		e.checked = form.chkAll.checked;
	}
}
//选项卡
function setTab(name,cursel,n){
	for(i=1;i<=n;i++){
		var menu=document.getElementById(name+i);
		var con=document.getElementById("con_"+name+"_"+i);
		menu.className=i==cursel?"hover":"";
		con.style.display=i==cursel?"block":"none";
	}
}
</script>
</body>